package com.list88zhu.annotation.room;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.list88zhu.android.room.User;
import com.list88zhu.android.room.UserDatabase;
import com.list88zhu.annotation.R;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.list88zhu.android.room.UserDatabase.*;

public class RoomActivity extends AppCompatActivity {

    private User user;
    private List<User> users = new ArrayList<>();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_room);
        findViewById(R.id.addBtn).setOnClickListener(this::onAddClick);
        findViewById(R.id.deleteBtn).setOnClickListener(this::onDeleteClick);
        findViewById(R.id.updateBtn).setOnClickListener(this::onUpdateClick);
    }

    private void onAddClick(View view) {
        user = new User("zhuyingming", new Date());
        users.add(user);
        getDefault(getApplicationContext()).getUserDao().insertAll(users);
    }

    private void onDeleteClick(View view){
        getDefault(getApplicationContext()).getUserDao().delete(user);
    }

    private void onUpdateClick(View view) {
        user.setName("android");
        getDefault(getApplicationContext()).getUserDao().update(user);
    }

    private void onQueryClick(View view) {
        List<User> userList = getDefault(getApplicationContext()).getUserDao().getUserAll();
    }
}
